package com.en.filter;

import com.en.common.ThreadLocalUtils;
import com.en.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: MyFilter
 * Package: com.tencent.wxcloudrun.MyFilter
 * Description:
 *
 * @Date: 2023/1/23 6:41 PM
 * @Author: wht
 */
@Slf4j
@Order(1)
@WebFilter(filterName = "MyFilter", urlPatterns = {"/*"})
public class MyFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println("----FilterDemo01过滤器初始化----");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        response.setHeader("Content-Type", "application/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "*");


        String requestURI = request.getRequestURI();
        log.info("请求路径：" + requestURI);


        filterChain.doFilter(servletRequest, servletResponse);

    }


    @Override
    public void destroy() {
        log.info("请求--------------------------end");
        ThreadLocalUtils.remove();
    }

}
